package A01Mangner;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class SMSupdate {
    public static void Menu(){
        System.out.println("欢迎来到学生管理系统");
        System.out.println("请选择操作： 1.登录 2.注册 3.忘记密码 ");
    }
    public static String Gene(){
        char[] eng=new char[52];
        for(int i=0;i<52;i++){
            if(i<26) {
                eng[i] = (char) ('A' + i);
            }
            else{
                eng[i] = (char) ('a' + i-26);
            }
        }
        Random ra=new Random();
        String code="";
        for(int i=0;i<4;i++){
            int underflag=ra.nextInt(52);
            code+=eng[underflag];
        }
        int number=ra.nextInt(10);
        code+=(char)('0'+number);
        char[] temparray=code.toCharArray();
        for(int i=0;i<5;i++){
            int raTemp=ra.nextInt(5);
            char temp=temparray[i];
            temparray[i]=temparray[raTemp];
            temparray[raTemp]=temp;
        }
        StringBuilder endCode=new StringBuilder();
        for(int i=0;i< temparray.length;i++){
            endCode.append(temparray[i]);
        }
        return endCode.toString();
    }
    public static StudentUpdate signIn(){
        Scanner temp=new Scanner(System.in);
        StudentUpdate student=new StudentUpdate();
        System.out.println("请输入用户名");
        String code=temp.next();
        boolean nameflag=student.setUsername(code);
        if(nameflag){
            System.out.println("请输入密码");
            String password=temp.next();
            System.out.println("请再次输入密码");
            String password1=temp.next();
            if(password.equals(password1)){
                student.setPassword(password);
                System.out.println("请输入身份证号");
                String IDcard=temp.next();
                boolean cardflag=student.setIDcard(IDcard);
                if(cardflag){
                    student.setIDcard(IDcard);
                    System.out.println("请输入手机号");
                    String phonenumber=temp.next();
                    boolean phoneflag=student.setUserphonenum(phonenumber);
                    if(phoneflag){
                        student.setUserphonenum(phonenumber);
                        return student;
                    }
                    else{
                        System.out.println("手机号格式错误");
                    }
                }
                else{
                    System.out.println("身份证号格式错误");
                }
            }
            else{
                System.out.println("两次密码不一致");
            }
        }
        return null;
    }
    public static boolean abroad(ArrayList<StudentUpdate> list){
        Scanner temp=new Scanner(System.in);
        System.out.println("请输入用户名");
        String namecode=temp.next();
        for(int i=0;i<list.size();i++){
            if(list.get(i).getUsername().equals(namecode)){
                System.out.println("请输入密码");
                String password=temp.next();
                if(list.get(i).getPassword().equals(password)){
                    String genecode=Gene();
                    System.out.println(genecode);
                    System.out.println("请输入验证码");
                    String code=temp.next();
                    if(genecode.equals(code)){
                        return true;
                    }
                    else{
                        System.out.println("验证码错误");
                        return false;
                    }
                }
                else{
                    System.out.println("密码错误");
                    return false;
                }
            }
        }
        System.out.println("用户名不存在，请注册");
        return false;
    }
    public static boolean forgetPassword(ArrayList<StudentUpdate> list){
        Scanner temp=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username=temp.next();
        for(int i=0;i<list.size();i++){
            if(list.get(i).getUsername().equals(username)){
                System.out.println("请输入身份证号");
                String IDcard=temp.next();
                if(list.get(i).getIDcard().equals(IDcard)){
                    System.out.println("请输入手机号");
                    String phonenum=temp.next();
                    if(list.get(i).getUserphonenum().equals(phonenum)){
                        System.out.println("请输入新的密码");
                        String password=temp.next();
                        System.out.println("请再次输入密码");
                        String password1=temp.next();
                        if(password.equals(password1)){
                            list.get(i).setPassword(password);
                            return true;
                        }
                        else{
                            System.out.println("两次密码不一致");
                        }
                    }
                    else{
                        System.out.println("手机号错误");
                        return false;
                    }
                }
                else{

                }
            }
        }
        System.out.println("未注册，请先注册");
        return false;
    }
    public static boolean IDCheck(ArrayList<StudentUpdate> arrayList, StudentUpdate studentUpdate){
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i).getUsername().equals(studentUpdate.getUsername())){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        boolean flag = true;
        Scanner choice = new Scanner(System.in);
        ArrayList<StudentUpdate> arrayList=new ArrayList<StudentUpdate>();
        while (flag){
            Menu();
            int choiceNum = choice.nextInt();
            switch (choiceNum){
                case 1:
                    if(abroad(arrayList)){
                        System.out.println("登录成功");
                    }
                    break;
                    case 2:
                        StudentUpdate studentuser=signIn();
                        if(IDCheck(arrayList,studentuser)){
                            System.out.println("注册完成");
                            arrayList.add(studentuser);
                        }else{
                            System.out.println("用户名重复，请重新注册");
                        }
                        break;
                        case 3:
                            if(forgetPassword(arrayList)){
                                System.out.println("密码修改成功");
                            }
                            break;
            }
        }
    }
}
